﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fuse8.ContentManagementFramework.Administration.Controls;
using Fuse8.ContentManagementFramework.Administration.Models;
using Fuse8.ContentManagementFramework.BizFacade;
using Fuse8.ContentManagementFramework.BizFacade.AdministrationServices;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Bases;
using Fuse8.ContentManagementFramework.Domain.Special;
using System.Xml.Serialization;
using Fuse8.DomainFramework.BusinessService;
using Telerik.Web.Mvc;

namespace Fuse8.ContentManagementFramework.Administration.Controllers
{
    [CmsAuthorize(UserGroups = PredefinedUserGroups.Developer)]
    public class CmsDomainController : CmsAdminController
    {
        private IEntityBusinessService<CmsDomain, int> _domainService = new BusinessServiceFacade<CmsDomain, int>();


        public virtual ActionResult Index(int? pageSize)
        {
            int currentPageSize = pageSize ?? DefaultPageSize;

            var data = _domainService.FindAll();

            ViewBag.PageSize = currentPageSize;
            ViewBag.Total = data.Count();

            return View(data);
        }

        [GridAction]
        public virtual ActionResult _Index(int? pageSize)
        {
            var data = _domainService.FindAll();

            return View(
                   new GridModel<CmsDomain>
                   {
                       Data = data
                   });
        }

        public ActionResult Add()
        {
            return View(new CmsAdminDomainModel());
        }

        [HttpPost]
        public ActionResult Add(CmsAdminDomainModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var domain = new CmsDomain()
                             {
                                 Hostname = model.Hostname,
                                 SiteId = model.SiteId.GetValueOrDefault()
                             };
            TrackingEntityObjectMediator.TrackEntity(domain);
            _domainService.Add(domain);

            return RedirectToAction("Index");
        }

        protected override bool _Delete(int id)
        {
            _domainService.Remove(id);
            return true;
        }


    }
}
